package com.practices.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Enumeration;

import org.json.JSONObject;

import android.content.Context;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

/**
 * android android设备信息类
 * 
 * @author 0907
 * 
 */

public class AndroidDeviceInfo {
	
	private String sysDateTime;// 设备系统时间
	private String deviceName;// 设备名称及型号
	private String deviceSoftwareVersion;// 软件版本
	private String line1Number;// 电话号码
	private String imei;// imei
	private String networkType;// 网络类型
	private String operatorName;// 运营商名称
	private String networkCountryIso;// 运营商编号
	private String phoneType;// 电话类型
	private String screenSize;// 屏幕尺寸
	private String isWifi;// 是否开通wifi
	private String userID;// 用户id
	
	private Location mLocation=null;

	/**
	 * 返回本地手机号码，这个号码不一定能获取到
	 * 
	 * @param context
	 * @return
	 */
	public static String getNativePhoneNumber(Context context) {
		TelephonyManager telephonyManager;
		telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String NativePhoneNumber = null;
		NativePhoneNumber = telephonyManager.getLine1Number();
		return NativePhoneNumber;
	}

	/**
	 * 返回手机服务商名字
	 * 
	 * @param context
	 * @return
	 */
	public static String getProvidersName(Context context) {
		String ProvidersName = null;
		// 返回唯一的用户ID;就是这张卡的编号神马的
		String IMSI = getIMSI(context);
		// IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信。
		System.out.println(IMSI);
		if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
			ProvidersName = "中国移动";
		} else if (IMSI.startsWith("46001")) {
			ProvidersName = "中国联通";
		} else if (IMSI.startsWith("46003")) {
			ProvidersName = "中国电信";
		} else {
			ProvidersName = "其他服务商";
		}
		return ProvidersName;
	}

	/**
	 * 返回手机IMSI号码
	 * 
	 * @param context
	 * @return
	 */
	public static String getIMSI(Context context) {
		TelephonyManager telephonyManager;
		telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		// 返回唯一的用户ID;就是这张卡的IMSI编号
		return telephonyManager.getSubscriberId();
	}

	/**
	 * 使用Wifi时获取IP 设置用户权限
	 * 
	 * <uses-permission
	 * android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
	 * 
	 * <uses-permission
	 * android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
	 * 
	 * <uses-permission
	 * android:name="android.permission.WAKE_LOCK"></uses-permission>
	 * 
	 * @return
	 */
	public static String getWifiIp(Context context) {
		// 获取wifi服务
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		// 判断wifi是否开启
		if (!wifiManager.isWifiEnabled()) {
			wifiManager.setWifiEnabled(true);
		}
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		int ipAddress = wifiInfo.getIpAddress();
		return intToIp(ipAddress);
	}

	private static String intToIp(int i) {
		return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF);

	}

	/**
	 * 使用GPRS上网，时获取ip地址，设置用户上网权限
	 * 
	 * <uses-permission
	 * android:name="android.permission.INTERNET"></uses-permission>
	 * 
	 * @return
	 */

	public static String getGPRSIp() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			// TALogger.d("IpUtil", ex.getMessage());
		}
		return "";
	}

	/**
	 * 根据context获取android设备信息
	 * 
	 * @param cx
	 * @return
	 */
	public static AndroidDeviceInfo getAndroidDeviceInfo(Context cx) {
		
		AndroidDeviceInfo milkAndroidInfo = new AndroidDeviceInfo();
		
		TelephonyManager tm = (TelephonyManager) cx.getSystemService(Context.TELEPHONY_SERVICE);
		Build bd = new Build();
		milkAndroidInfo.setDeviceName(bd.MODEL);
		milkAndroidInfo.setDeviceSoftwareVersion(tm.getDeviceSoftwareVersion());
		milkAndroidInfo.setImei(tm.getDeviceId());
		milkAndroidInfo.setIsWifi(String.valueOf(isWiFiActive(cx)));
		milkAndroidInfo.setLine1Number(tm.getLine1Number());
		milkAndroidInfo.setNetworkCountryIso(tm.getNetworkCountryIso());
		milkAndroidInfo.setNetworkType(String.valueOf(tm.getNetworkType()));
		milkAndroidInfo.setOperatorName(tm.getNetworkOperatorName());
		milkAndroidInfo.setPhoneType(String.valueOf(tm.getPhoneType()));
		milkAndroidInfo.setScreenSize(getDisplayMetrics(cx));
		milkAndroidInfo.setSysDateTime(getDateXX());
		milkAndroidInfo.setUserID("");

		return milkAndroidInfo;
	}

	/**
	 * 获取屏幕尺寸
	 * 
	 * @param cx
	 * @return
	 */
	public static String getDisplayMetrics(Context cx) {
		String str = "";
		DisplayMetrics dm = new DisplayMetrics();
		dm = cx.getApplicationContext().getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;
		int screenHeight = dm.heightPixels;
		float density = dm.density;
		float xdpi = dm.xdpi;
		float ydpi = dm.ydpi;

		str += "The absolute width:" + String.valueOf(screenWidth) + "pixels\n";
		str += "The absolute heightin:" + String.valueOf(screenHeight) + "pixels\n";
		str += "The logical density of the display.:" + String.valueOf(density) + "\n";
		str += "X dimension :" + String.valueOf(xdpi) + "pixels per inch\n";
		str += "Y dimension :" + String.valueOf(ydpi) + "pixels per inch\n";

		return String.valueOf(screenWidth) + "*" + String.valueOf(screenHeight);
	}

	// 得到日期详细
	public static String getDateXX() {
		Date dNow = new Date(System.currentTimeMillis());
		SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm");
		String date = formatter.format(dNow);

		return date;
	}

	/**
	 * 是否支持wifi
	 * 
	 * @param inContext
	 * @return
	 */
	public static boolean isWiFiActive(Context inContext) {
		try {
			Context context = inContext.getApplicationContext();
			ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null) {
				NetworkInfo[] info = connectivity.getAllNetworkInfo();
				if (info != null) {
					for (int i = 0; i < info.length; i++) {
						if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) {
							return true;
						}
					}
				}
			}
		} catch (Exception ex) {
			return false;
		}
		return false;
	}

	/**
	 * 获取字符串形式
	 */
	public String toJsonString() {
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append("\"deviceName\":");
		sb.append("\"");
		sb.append(deviceName);
		sb.append("\"");
		sb.append(",");
		sb.append("\"deviceSoftwareVersion\":");
		sb.append("\"");
		sb.append(deviceSoftwareVersion);
		sb.append("\"");
		sb.append(",");
		sb.append("\"line1Number\":");
		sb.append("\"");
		sb.append(line1Number);
		sb.append("\"");
		sb.append(",");
		sb.append("\"imei\":");
		sb.append("\"");
		sb.append(imei);
		sb.append("\"");
		sb.append(",");
		sb.append("\"networkType\":");
		sb.append("\"");
		sb.append(networkType);
		sb.append("\"");
		sb.append(",");
		sb.append("\"operatorName\":");
		sb.append("\"");
		sb.append(operatorName);
		sb.append("\"");
		sb.append(",");
		sb.append("\"networkCountryIso\":");
		sb.append("\"");
		sb.append(networkCountryIso);
		sb.append("\"");
		sb.append(",");
		sb.append("\"phoneType\":");
		sb.append("\"");
		sb.append(phoneType);
		sb.append("\"");
		sb.append(",");
		sb.append("\"screenSize\":");
		sb.append("\"");
		sb.append(screenSize);
		sb.append("\"");
		sb.append(",");

		sb.append("\"isWifi\":");
		sb.append("\"");
		sb.append(isWifi);
		sb.append("\"");
		sb.append(",");
		sb.append("\"userID\":");
		sb.append("\"");
		sb.append(userID);
		sb.append("\"");
		sb.append(",");
		sb.append("\"sysDateTime\":");
		sb.append("\"");
		sb.append(sysDateTime);
		sb.append("\"");
		sb.append("}");

		return sb.toString();

	}

	public String getSysDateTime() {
		return sysDateTime;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public String getDeviceSoftwareVersion() {
		return deviceSoftwareVersion;
	}

	public String getLine1Number() {
		return line1Number;
	}

	public String getImei() {
		return imei;
	}

	public String getNetworkType() {
		return networkType;
	}

	public String getOperatorName() {
		return operatorName;
	}

	public String getNetworkCountryIso() {
		return networkCountryIso;
	}

	public String getPhoneType() {
		return phoneType;
	}

	public String getScreenSize() {
		return screenSize;
	}

	public String getUserID() {
		return userID;
	}

	public void setSysDateTime(String sysDateTime) {
		this.sysDateTime = sysDateTime;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}

	public void setDeviceSoftwareVersion(String deviceSoftwareVersion) {
		this.deviceSoftwareVersion = deviceSoftwareVersion;
	}

	public void setLine1Number(String line1Number) {
		this.line1Number = line1Number;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public void setNetworkType(String networkType) {
		this.networkType = networkType;
	}

	public void setOperatorName(String operatorName) {
		this.operatorName = operatorName;
	}

	public void setNetworkCountryIso(String networkCountryIso) {
		this.networkCountryIso = networkCountryIso;
	}

	public void setPhoneType(String phoneType) {
		this.phoneType = phoneType;
	}

	public void setScreenSize(String screenSize) {
		this.screenSize = screenSize;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}

	public String getIsWifi() {
		return isWifi;
	}

	public void setIsWifi(String isWifi) {
		this.isWifi = isWifi;
	}

}
